namespace Sustainalytics.ClientDW.Synchronization
{
    using System;
    using System.Threading.Tasks;

    public static class RetryPolicy
    {
        public static async Task<bool> DoAsync(Func<Task<bool>> verifiableAction, int millisecondsDelay = 2000, int numberOfRetries = 5)
        {
            int retryTimes = 1;
            bool successful = false;

            while (!successful && retryTimes <= numberOfRetries)
            {
                try
                {
                    var result = await verifiableAction.Invoke().ConfigureAwait(false);
                    if (!result)
                    {
                        retryTimes++;
                        await Task.Delay(millisecondsDelay).ConfigureAwait(false);
                    }
                    else
                    {
                        successful = true;
                    }
                }
                catch
                {
                    retryTimes++;
                }
            }

            return successful;
        }
    }
}
